﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ApiDoc
{
    partial class ApiDocumentationManager
    {
        /// <summary>
        /// Returns the ParameterDocumentation for <paramref name="parameter"/>.
        /// </summary>
        /// <remarks>
        /// If the assembly containing <paramref name="parameter"/> is not already part of the documentation managed by this manager
        /// it will be added with AddAssembly() before generating the documentation for <paramref name="parameter"/>.
        /// </remarks>
        /// <param name="type"></param>
        /// <returns></returns>
        public ParameterDocumentation GetParameterDocumentation(ParameterInfo parameter)
        {
            // Try the cache first.
            if (m_parameters.ContainsKey(parameter)) {
                return m_parameters[parameter];
            }

            // Add the assembly for the type containing  we don't already have it included.
            if (!m_assemblies.Contains(parameter.Member.DeclaringType.Assembly)) {
                AddAssembly(parameter.Member.DeclaringType.Assembly);
            }

            // Cache the result using the type directly.
            var ret = new ParameterDocumentation(parameter, this);
            m_parameters.Add(parameter, ret);

            return ret;
        }

        private Dictionary<ParameterInfo, ParameterDocumentation> m_parameters = new Dictionary<ParameterInfo, ParameterDocumentation>();
    }
}
